package com.zhichat.infrastructure.util;

import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.google.common.collect.Lists;
import com.zhichat.infrastructure.constants.Constants;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class Utils {

    /**
     * 获取request的url字符串
     */
    public static String requestInfo(HttpServletRequest request) {
        String requestUrl = request.getRequestURI();
        String method = request.getMethod();
        return method + " " + requestUrl;
    }

    /**
     * 生成唯一ID
     */
    public static String uniqueId() {
        return ObjectId.next();
    }

    /**
     * 获取雪花ID
     */
    public static Long snowflakeId() {
        return IdUtil.getSnowflakeNextId();
    }

    /**
     * 拼接用户ID
     */
    public static String getUserId(String localDate_yyyy_MM_dd) {
        int random = (int) (Math.random() * 2) + 7;
        String simpleUUID = IdUtil.simpleUUID();
        String uuid = simpleUUID.substring(simpleUUID.length() - random);
        return Constants.USER_ID_PREFIX.concat(localDate_yyyy_MM_dd).concat(uuid);
    }

    public static String concatTimeStart(String yyyy_MM_dd) {
        return yyyy_MM_dd + " 00:00:00";
    }

    public static String concatTimeEnd(String yyyy_MM_dd) {
        return yyyy_MM_dd + " 23:59:59";
    }

    /**
     * Long 数组排序后，拼接成字符串，再进行MD5
     */
    public static String lsMd5(Long... ls) {
        if (ls == null || ls.length == 0) {
            return null;
        }
        Arrays.sort(ls);
        return SecureUtil.md5(Lists.newArrayList(ls).stream().map(Object::toString).collect(Collectors.joining("")));
    }

    /**
     * html 标签处理
     */
    public static String cleanHtmlTagHandling1(String html) {
        if (StrUtil.isBlank(html)) {
            return html;
        }
        return html.replaceAll("<.*?>", "");
    }

    /**
     * html 标签处理
     */
    public static String cleanHtmlTagHandling(String html) {
        if (StrUtil.isBlank(html)) {
            return html;
        }
        return html.replaceAll("<", "&lt;")
                .replaceAll("\r\n", "<br>")
                .replaceAll("\n", "<br>");
    }

    /**
     * 获取文件后缀
     */
    public static String getFileSuffix(String fileName) {
        if (StrUtil.isBlank(fileName)) {
            return "";
        }
        int indexOf = fileName.lastIndexOf(".");
        if (-1 == indexOf) {
            return "";
        }
        return fileName.substring(indexOf + 1);
    }

    /**
     * MB 转换为 字节
     */
    public static Long mb2ByteNumber(Long mb_number) {
        if (Objects.isNull(mb_number)) {
            return 0L;
        }
        return mb_number * Constants.NUM_1024 * Constants.NUM_1024;
    }

    /**
     * GB 转换为 字节
     */
    public static Long gb2ByteNumber(Long gb_number) {
        if (Objects.isNull(gb_number)) {
            return 0L;
        }
        return gb_number * Constants.NUM_1024 * Constants.NUM_1024 * Constants.NUM_1024;
    }
}
